package main

import (
	"fmt"
	"io"
	"log"
	"net"
	"os"
	"time"
)

func handleConn(c net.Conn) {
	defer c.Close()
	_, err := io.WriteString(c, time.Now().Format("00:00:00\n"))
	if err != nil {
		return
	}
	time.Sleep(1 * time.Second)

}

func main() {
	var port string
	if len(os.Args) < 2 {
		port = "8000"
	} else {
		port = os.Args[1]
	}
	listener, err := net.Listen("tcp", fmt.Sprintf("localhost:%s", port))
	if err != nil {
		log.Fatal(err)
	}
	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Print(err)
			continue
		}
		go handleConn(conn)
	}
}
